package annotation.util;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import annotation.annotations.Column;
import annotation.annotations.Table;

public class TableCreateSqlResolver {

	public static Map<String, String> resolve(List<Class<?>> classes2resolve, String suffix) {
		Map<String, String> classNameSqlMap = new HashMap<>();
		for (Class<?> classObj : classes2resolve) {
			try {
				Map<String, String> tablePropertiesMap = resolveTableProperties(classObj, suffix);
				String tableCreateSql = "create table `" + tablePropertiesMap.get("tableName") + "`( "
						+ resolveColumnProperties(classObj) + ") " + tablePropertiesMap.get("otherProperties");
				classNameSqlMap.put(classObj.getSimpleName(), tableCreateSql);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return classNameSqlMap;
	}

	public static Map<String, String> resolveTableProperties(Class<?> classObj, String suffix) throws Exception {
		Map<String, String> tablePropertiesMap = new HashMap<>();
		String tableName = "";
		Table tableAnno = classObj.getAnnotation(Table.class);
		if (tableAnno == null)
			throw new Exception("此类没有声明用于建立数据表的注解");
		tableName = tableAnno.value().equals("") ? suffix + "_" + classObj.getSimpleName().toLowerCase()
				: tableAnno.value();
		tablePropertiesMap.put("tableName", tableName);
		tablePropertiesMap.put("otherProperties",
				"ENGINE=" + tableAnno.engine() + " DEFAULT CHARSET=" + tableAnno.charSet());
		return tablePropertiesMap;
	}

	public static String resolveColumnProperties(Class<?> classObj) throws Exception {
		StringBuffer colSqlBuffer = new StringBuffer();
		Field[] fields = classObj.getDeclaredFields();
		if (fields == null || fields.length == 0)
			throw new Exception("此类没有定义字段");
		for (Field field : fields) {
			Column columnAnno = field.getAnnotation(Column.class);
			if (columnAnno == null)
				continue;
			String columnName = columnAnno.name();
			if (StringUtils.isEmpty(columnName))
				columnName = field.getName().toLowerCase();
			colSqlBuffer.append("`" + columnName + "` ");
			colSqlBuffer.append(columnAnno.type());
			if (columnAnno.length() != -1)
				colSqlBuffer.append("(" + columnAnno.length() + ") ");
			if(StringUtils.isNotEmpty(columnAnno.collate()))
				colSqlBuffer.append("COLLATE ").append(columnAnno.collate()).append(" ");
			if (columnAnno.isNotNull() == true)
				colSqlBuffer.append("NOT NULL ");
			if(StringUtils.isNotEmpty(columnAnno.defaultValue()))
				colSqlBuffer.append("DEFAULT '").append(columnAnno.defaultValue()).append("' ");
			if(StringUtils.isNotEmpty(columnAnno.comment()))
					colSqlBuffer.append("COMMENT '").append(columnAnno.comment()).append("' ");
			if (columnAnno.isPrimatyKey() == true)
				colSqlBuffer.append("PRIMARY KEY ");
			colSqlBuffer.append(",");
		}
		String colSql = colSqlBuffer.toString();
		if (colSql.equals(""))
			throw new Exception("此类没有声明用于建立数据表字段的注解");
		else
			return colSql.substring(0, colSql.length() - 1);
	}

	public static void main(String[] args) {
//		List<Class<?>> classList = new ArrayList<>();
//		classList.add(User.class);
//		System.out.println(resolve(classList, "table").get("User"));
//		System.out.println("".length());
	}
}
